package org.rapan;

import java.util.Set;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.rapan.jpa.Consultant;

public class EmailUtilities {

	public static void sendFailedReportEmail(long consultantId,
			Set<String> errorMessages) {
		QueueConnection cnn = null;
		QueueSender sender = null;
		QueueSession sessioner = null;
		javax.jms.Queue queue = null;
		QueueConnectionFactory factory = null;

		try {
			// creates a connection to the Emailqueue
			InitialContext ctx = new InitialContext();
			queue = (javax.jms.Queue) ctx.lookup("/queue/EmailQueue");
			factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			cnn = factory.createQueueConnection();
			sessioner = cnn.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);

			sender = sessioner.createSender(queue);
			// Creates a message and fills all the needed attributes for the
			// emal
			ObjectMessage msg = sessioner.createObjectMessage();
			msg.setStringProperty("Name", "fReport");
			msg.setLongProperty("idConsultant", consultantId);
			StringBuilder content = new StringBuilder();


			for (String str : errorMessages) {
				content.append(str + "\n");
			}
			msg.setStringProperty("errorList", content.toString());

			sender.send(msg);

		} catch (NamingException e) {

			e.printStackTrace();
		} catch (JMSException e) {

			e.printStackTrace();
		} finally {
			try {
				cnn.close();
				sender.close();
				sessioner.close();

			} catch (JMSException e) {
				e.printStackTrace();
			}

		}

	}

	public static void sendSuccessfulReportEmail(long reportId) {
		QueueConnection cnn = null;
		QueueSender sender = null;
		QueueSession sessioner = null;
		javax.jms.Queue queue = null;
		QueueConnectionFactory factory = null;

		try {
			// creates a connection to the Emailqueue
			InitialContext ctx = new InitialContext();
			queue = (javax.jms.Queue) ctx.lookup("/queue/EmailQueue");
			factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			cnn = factory.createQueueConnection();
			sessioner = cnn.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);

			sender = sessioner.createSender(queue);
			// Creates a message and fills all the needed attributes for the
			// emal
			ObjectMessage msg = sessioner.createObjectMessage();
			msg.setStringProperty("Name", "sReport");
			msg.setLongProperty("reportID", reportId);

			sender.send(msg);

		} catch (JMSException e) {

			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();

		} finally {
			try {
				cnn.close();
				sender.close();
				sessioner.close();

			} catch (JMSException e) {
				e.printStackTrace();
			}

		}

	}
	
	public static void sendSuccessfulConsultantRegistrationEmail(Consultant consultant, String url) {
		QueueConnection cnn = null;
		QueueSender sender = null;
		QueueSession sessioner = null;
		javax.jms.Queue queue = null;
		QueueConnectionFactory factory = null;

		try {
			// creates a connection to the Emailqueue
			InitialContext ctx = new InitialContext();
			queue = (javax.jms.Queue) ctx.lookup("/queue/EmailQueue");
			factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			cnn = factory.createQueueConnection();
			sessioner = cnn.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);

			sender = sessioner.createSender(queue);
			// Creates a message and fills all the needed attributes for the
			// emal
			ObjectMessage msg = sessioner.createObjectMessage();
			msg.setStringProperty("Name", "consultEmail");
			msg.setStringProperty("email", consultant.getEmail());
			msg.setStringProperty("url", url);

			msg.setStringProperty("fullName", consultant.getFullName());
			msg.setStringProperty("password", consultant.getPassword());

			sender.send(msg);

		} catch (JMSException e) {

			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();

		} finally {
			try {
				cnn.close();
				sender.close();
				sessioner.close();

			} catch (JMSException e) {
				e.printStackTrace();
			}

		}

	}

}
